﻿namespace home_storage
{
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Metadata;

    public static class StorageDbContextModel
    {
        public static void Configure(ModelBuilder modelBuilder)
        {
            // 配置Category实体
            modelBuilder.Entity<Category>(entity =>
            {
                // 主键配置
                entity.HasKey(e => e.Id);

                // 属性配置
                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(100);

                entity.Property(e => e.Path)
                    .IsRequired()
                    .HasMaxLength(200);

                entity.Property(e => e.AllowedExtensions)
                    .HasMaxLength(200);
            });

            // 配置FileMetadata实体
            modelBuilder.Entity<FileMetadata>(entity =>
            {
                // 主键配置
                entity.HasKey(e => e.Id);

                // 属性配置
                entity.Property(e => e.OriginalName)
                    .IsRequired()
                    .HasMaxLength(255);

                entity.Property(e => e.Path)
                    .IsRequired()
                    .HasMaxLength(500);

                entity.Property(e => e.Size)
                    .IsRequired();

                entity.Property(e => e.ContentType)
                    .HasMaxLength(100);

                entity.Property(e => e.UploadedAt)
                    .IsRequired();

                entity.Property(e => e.Description)
                    .HasMaxLength(1000);

                // 外键关系
                entity.HasOne(e => e.Category)
                    .WithMany()
                    .HasForeignKey(e => e.CategoryId)
                    .OnDelete(DeleteBehavior.Restrict); // 或 Cascade 根据业务需求
            });

            // 配置Tag实体
            modelBuilder.Entity<Tag>(entity =>
            {
                // 主键配置
                entity.HasKey(e => e.Id);

                // 属性配置
                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(50);
            });

            // 配置多对多关系（FileMetadata和Tag）
            modelBuilder.Entity<FileMetadata>()
                .HasMany(f => f.Tags)
                .WithMany(t => t.Files)
                .UsingEntity<Dictionary<string, object>>(
                    "FileTags",
                    j => j.HasOne<Tag>()
                        .WithMany()
                        .HasForeignKey("TagsId")
                        .OnDelete(DeleteBehavior.Cascade),
                    j => j.HasOne<FileMetadata>()
                        .WithMany()
                        .HasForeignKey("FilesId")
                        .OnDelete(DeleteBehavior.Cascade),
                    j =>
                    {
                        j.ToTable("FileTags");
                        j.HasKey("FilesId", "TagsId"); // 复合主键
                    });

            // 如果需要，可以添加索引
            modelBuilder.Entity<FileMetadata>()
                .HasIndex(f => f.CategoryId);

            modelBuilder.Entity<FileMetadata>()
                .HasIndex(f => f.UploadedAt);

            modelBuilder.Entity<Tag>()
                .HasIndex(t => t.Name)
                .IsUnique(); // 确保标签名唯一
        }
        public static IModel Instance
        {
            get
            {
                var builder = new DbContextOptionsBuilder<StorageDbContext>();
                builder.UseSqlite("Data Source=storage.db");

                // 配置模型
                builder.UseModel(GetModel());

                using var context = new StorageDbContext(builder.Options);
                return context.Model;
            }
        }

        private static IModel GetModel()
        {
            var builder = new ModelBuilder();

            // 在这里手动配置您的模型
            builder.Entity<Category>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired();
                entity.Property(e => e.Path).IsRequired();
            });

            builder.Entity<FileMetadata>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.HasOne(e => e.Category)
                    .WithMany()
                    .HasForeignKey(e => e.CategoryId);

                entity.HasMany(e => e.Tags)
                    .WithMany(e => e.Files)
                    .UsingEntity<Dictionary<string, object>>(
                        "FileTags",
                        j => j.HasOne<Tag>().WithMany().HasForeignKey("TagsId"),
                        j => j.HasOne<FileMetadata>().WithMany().HasForeignKey("FilesId"),
                        j => j.ToTable("FileTags"));
            });

            builder.Entity<Tag>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired();
            });

            return (IModel)builder.Model;
        }
    }
}
